package com.example.communitypro.util;

import java.text.ParseException;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DateUtil {
    /**
     * 获取当前日期年月日时分秒yyyy-MM-dd HH:mm:ss
     *
     * @return
     */
    public static String getDateTime() {
        SimpleDateFormat simpleFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date now = new Date();
        return simpleFormat.format(now);

    }

    /**
     *　获取当前日期年月日时分秒yyyy-MM-dd
     */
    public static String getDateYMD() {
        SimpleDateFormat simpleFormat = new SimpleDateFormat("yyyy-MM-dd");
        Date now = new Date();
        return simpleFormat.format(now);

    }
    /**
     *　将string日期 转为 date
     */
    public static Date StrToDate(String str) {


        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = null;
        try {
            date = format.parse(str);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date;
    }

    public static void main(String[] args) {
        Date date = StrToDate("2021-06-25 09:31:51");
        System.out.println(date);

    }


    /**
     *　将string日期 转为 date
     */
    public static Date StrToDateOwn(String str) {


        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        Date date = null;
        try {
            date = format.parse(str);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date;
    }

    /**
     *　将now日期 转为 yyyy-MM-dd
     */
    public static Date DateToDate(Date now) {

        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        String dateString = formatter.format(now);

        Date date = null;

        try {
            date = formatter.parse(dateString);

        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date;
    }

    /**
     * @Title: getTimeByMOnthAfterDay   参数时间date 转为对应格式
     * @param: @param date        要转换的时间
     * @param: @param formatstring        格式"yyyy-MM-dd HH:mm:ss", "yyyy-M-d H:m:s", "yyyy-MM-dd H:m:s", "yyyy-M-d HH:mm:ss"
     * @param: @return
     * @return: String
     */
    public static String getTimeByTransformation(Date date, String formatstring) {

        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        SimpleDateFormat simpleFormat = new SimpleDateFormat(formatstring);
        return  simpleFormat.format(cal.getTime());
    }

    /**
     * 参数时间当月最后一天
     * @param: @param date        要转换的时间
     * @param: @param formatstring        格式"yyyy-MM-dd HH:mm:ss", "yyyy-M-d H:m:s", "yyyy-MM-dd H:m:s", "yyyy-M-d HH:mm:ss"
     */
    public static String getTimeByMOnthAfterDay(Date date, String formatstring) {

        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.set(Calendar.DAY_OF_MONTH,cal.getActualMaximum(Calendar.DAY_OF_MONTH));
        SimpleDateFormat simpleFormat = new SimpleDateFormat(formatstring);
        return  simpleFormat.format(cal.getTime());
    }

    /**
     * 参数时间当月第一天
     ** @param: @param date        要转换的时间
     * @param: @param formatstring        格式"yyyy-MM-dd HH:mm:ss", "yyyy-M-d H:m:s", "yyyy-MM-dd H:m:s", "yyyy-M-d HH:mm:ss"
     */
    public static String getTimeByMOnthFirstDay(Date date, String formatstring) {

        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.set(Calendar.DAY_OF_MONTH, 1);
        SimpleDateFormat simpleFormat = new SimpleDateFormat(formatstring);
        return  simpleFormat.format(cal.getTime());

    }
}
